package com.example.springbooteasycode.zjrc.dns;

import java.net.*;
import java.io.*;

public class DNSFCClient {
    public static void main(String[] args) {
        // 检查输入参数
        if (args.length != 1) {
            System.out.println("请提供十六进制字符串作为参数，例如: java DNSFCClient 000100");
            return;
        }

        String hexInput = args[0];
        System.out.println("输入的十六进制字符串: " + hexInput);

        try {
            // 将十六进制字符串转换为byte数组
            byte[] inputBytes = hexStringToByteArray(hexInput);

            // FC服务器的域名和端口（请根据实际情况修改）
            String fcDomain = "www.baidu.com"; // 替换为实际的FC服务器域名
            int port = 53; // 端口号，根据实际服务端口修改

            // 通过DNS解析获取FC服务器的IP地址
            InetAddress[] addresses = InetAddress.getAllByName(fcDomain);
            if (addresses.length == 0) {
                System.out.println("无法解析FC服务器域名: " + fcDomain);
                return;
            }
            System.out.println("DNS 解析结果 for " + fcDomain + "：");
            for (InetAddress addr : addresses) {
                System.out.println("  IP 地址：" + addr.getHostAddress());
            }

            // 选择第一个IP地址进行连接
            InetAddress serverAddress = addresses[0];
            System.out.println("连接到FC服务器: " + serverAddress.getHostAddress() + ":" + port);

            // 创建Socket连接
            try (Socket socket = new Socket(serverAddress, port);
                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                 DataInputStream in = new DataInputStream(socket.getInputStream())) {

                // 发送数据
                out.write(inputBytes);
                out.flush();
                System.out.println("发送的数据 (十六进制): " + hexInput);

                // 接收响应（根据实际情况调整缓冲区大小）
                byte[] buffer = new byte[1024];
                int bytesRead = in.read(buffer);

                if (bytesRead > 0) {
                    // 截取实际接收到的数据
                    byte[] responseBytes = new byte[bytesRead];
                    System.arraycopy(buffer, 0, responseBytes, 0, bytesRead);

                    // 将响应转换为十六进制字符串并打印
                    String hexOutput = byteArrayToHexString(responseBytes);
                    System.out.println("接收的响应 (十六进制): " + hexOutput);
                } else {
                    System.out.println("未接收到任何数据");
                }
            }

        } catch (UnknownHostException e) {
            System.err.println("无法解析主机: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("网络通信错误: " + e.getMessage());
        }
    }

    /**
     * 将十六进制字符串转换为byte数组
     */
    public static byte[] hexStringToByteArray(String s) {
        if (s == null || s.length() % 2 != 0) {
            throw new IllegalArgumentException("无效的十六进制字符串");
        }

        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    /**
     * 将byte数组转换为十六进制字符串
     */
    public static String byteArrayToHexString(byte[] bytes) {
        if (bytes == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }
}

